//
//  ZWAttributeStr.m
//  TopicDemo
//
//  Created by InitialC on 16/11/28.
//  Copyright © 2016年 InitialC. All rights reserved.
//

#import "ZWAttributeStr.h"

@implementation ZWAttributeStr

#pragma mark -- 设置在一个文本中所有特殊字符的特殊颜色

+ (NSMutableAttributedString *)setAllText:(NSString *)allStr andSpcifiStr:(NSString *)specifiStr withColor:(UIColor *)color specifiStrFont:(UIFont *)font {
    
    NSMutableAttributedString *mutableAttributedStr = [[NSMutableAttributedString alloc] initWithString:allStr];
    if (color == nil) {
        color = [UIColor redColor];
    }
    if (font == nil) {
        font = [UIFont systemFontOfSize:17.];
    }
    //    NSArray *array = [allStr componentsSeparatedByString:specifiStr];//array.cout-1是所有字符特殊字符出现的次数
    NSRange searchRange = NSMakeRange(0, [allStr length]);
    NSRange range;
    //拿到所有的相同字符的range
    while
        ((range = [allStr rangeOfString:specifiStr options:0 range:searchRange]).location != NSNotFound) {
            //改变多次搜索时searchRange的位置
            searchRange = NSMakeRange(NSMaxRange(range), [allStr length] - NSMaxRange(range));
            //设置富文本
            [mutableAttributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
            [mutableAttributedStr addAttribute:NSFontAttributeName value:font range:range];
        }
    return mutableAttributedStr;
}


@end
